/**
 * <p>文件名:		LogSignalHandler.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		千方集团CTFO</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.services.jmx;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

import com.caits.lbs.framework.log.CommonLogFactory;

import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 * <p>
 * LogSignalHandler
 * </p>
 * <p>
 * 用途：系统信号处理类,这里用作日志级别修改
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2013-8-6
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2013-8-6 上午9:48:03</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2013-8-6 上午9:48:03</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
@SuppressWarnings("restriction")
public class LogSignalHandler implements SignalHandler,InitializingBean {

	/** 日志级别管理类 */
	private final LogLevelAnnotation logLevel = new LogLevelAnnotation();
	
	/** 默认信号名称 */
	private String sigName="USR2";
	
	/** 日志类 */
	private static Logger log = CommonLogFactory.getLog();

	/**
	 * 处理信号
	 * 
	 * @param sn
	 */
	private void signalCallback(Signal sn) {
		log.info("signam="+sn.getName() + ",signNum="+sn.getNumber()+" have recevied.");
		if (logLevel.getLevel() == Level.INFO)
			logLevel.setDebug();
		else
			logLevel.setInfo();
		log.debug("debug info could be outputed.");
	}

	@Override
	public void handle(Signal signalName) {
		signalCallback(signalName);
	}

	/**
	 * 获取变量<code>sigName</code>的值
	 * @return 返回的数据类型是<code>String</code>
	 */
	public String getSigName() {
		return sigName;
	}

	/**
	 * 设置变量<code> sigName</code> 的值
	 * @param sigName  <code>sigName</code> 参数类型是<code>String</code>
	 */
	public void setSigName(String sigName) {
		this.sigName = sigName;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		JMXManager jmx = JMXManager.getInstance();
		if(jmx!=null)jmx.addObject(logLevel);
		Signal sig = new Signal(getSigName());
		Signal.handle(sig, this);
	}

}